package com.esun.rabbit2.communicaters.recharg;

import java.util.Hashtable;

import com.esun.config.Key;
import com.esun.util.log.EDebug;

/** 
* @ClassName: PostAndAnalysisEr 
* @Description: 返回数据解析类  
* @author Miko baoz@500wan.com
* @date 2011-1-13 下午09:41:20 
*  
*/
public class PostAndAnalysisEr extends DataPackageBuilder{

	
	private byte[] rdyData;
	private byte[] head1 = new byte[DataShare.FIRSTHEADLENGTH + 5];
	private byte[] head2 = new byte[DataShare.SECONDHEADLENGTH + 5];
//	private ArrayList<String[]> alAnalysisResult;
//	private byte[] body = new byte[rdyData.length - DataShare.FIRSTHEADLENGTH
//			- DataShare.SECONDHEADLENGTH];

	private ByteUtils byteUtils = new ByteUtils();

	private Hashtable<Object, Object> htHead1 = new Hashtable<Object, Object>();
	private Hashtable<Object, Object> htHead2 = new Hashtable<Object, Object>();
	
	
	
	public PostAndAnalysisEr(String userSession, String strBody, short wMFuncNo,
			byte compress, byte encrypt) {
		super(userSession, strBody, wMFuncNo, compress, encrypt);
		// TODO Auto-generated constructor stub
		
		// 向服务器发送信息，获得服务器返回的未解析的byte[]
		this.rdyData = ServerCommunicater.doPost(super.getDataPackage());
		this.getHead1Info();
		this.getHead2Info();
	}
	
	

	/**
	 * <p>获得解析后head1部分信息</p>
	 * 
	 * 解析后会放到 byte[] head1中
	 * 
	 * @author Miko
	 */
	private void getHead1Info() {

		int recProtocolVersion, recClientSoftVersion, recBodyLength, recCompress, recEncrypt, recChecksum, recTimeStamp;

		System.arraycopy(rdyData, 0, head1, 0, DataShare.FIRSTHEADLENGTH );

		int pos = 0;
		recProtocolVersion = byteUtils.bytes2Integer(head1, pos);
		pos += 4;
		// recClientSoftVersion = byteUtils.bytes2Integer(head1, pos);
		recClientSoftVersion = byteUtils.bytes2Short(head1, pos);
		pos += 2;
		recBodyLength = byteUtils.bytes2Integer(head1, pos);
		pos += 4;
		recCompress = head1[pos];
		pos += 1;
		recEncrypt = head1[pos];
		pos += 1;
		recChecksum = byteUtils.bytes2Integer(head1, pos);
		pos += 4;
		recTimeStamp = byteUtils.bytes2Integer(head1, pos);
		pos += 2;

//		System.out.println("recProtocolVersion   -->  " + recProtocolVersion);
//		System.out.println("recClientSoftVersion -->  " + recClientSoftVersion);
//		System.out.println("recBodyLength        -->  " + recBodyLength);
//		System.out.println("recCompress          -->  " + recCompress);
//		System.out.println("recEncrypt           -->  " + recEncrypt);
//		System.out.println("recChecksum          -->  " + recChecksum);
//		System.out.println("recTimeStamp         -->  " + recTimeStamp);

		htHead1.put(Key.KEY_RECPROTOCOLVERSION, recProtocolVersion);
		htHead1.put(Key.KEY_RECCLIENTSOFTVERSION, recClientSoftVersion);
		htHead1.put(Key.KEY_RECBODYLENGTH, recBodyLength);
		htHead1.put(Key.KEY_RECCOMPRESS, recCompress);
		htHead1.put(Key.KEY_RECENCRYPT, recEncrypt);
		htHead1.put(Key.KEY_RECCHECKSUM, recChecksum);
		htHead1.put(Key.KEY_RECTIMESTAMP, recTimeStamp);
	}

	
	/**
	 * <p>获得解析后head2部分信息</p>
	 * 
	 * 解析后会放到 byte[] head2中
	 * 
	 * @author Miko
	 */
	private void getHead2Info() {

		String recUserID, recSessionID;
		short recPlaytype, recMFuncNo;
		byte recStatus;
		int recDownLoadNo;
		int posHd2 = 0;

		System.arraycopy(rdyData, DataShare.FIRSTHEADLENGTH, head2, 0, DataShare.SECONDHEADLENGTH );

		recUserID = ByteUtils.bytesToString(head2, posHd2, 40);
		posHd2 += 40;
		recSessionID = ByteUtils.bytesToString(head2, posHd2, 100);
		posHd2 += 100;
		recPlaytype = byteUtils.bytes2Short(head2, posHd2);
		posHd2 += 2;
		recMFuncNo = byteUtils.bytes2Short(head2, posHd2);
		posHd2 += 2;
		recStatus = head2[posHd2];
		posHd2++;
		recDownLoadNo = byteUtils.bytes2Integer(head2, posHd2);
		posHd2 += 2;

		recUserID = byteUtils.removeByte0(recUserID);
		recSessionID = byteUtils.removeByte0(recSessionID);

//		System.out.println("recUserID      -->  " + recUserID);
//		System.out.println("recSessionID   -->  " + recSessionID);
//		System.out.println("recPlaytype    -->  " + recPlaytype);
//		System.out.println("recMFuncNo     -->  " + recMFuncNo);
//		System.out.println("recStatus      -->  " + recStatus);
//		System.out.println("recDownLoadNo  -->  " + recDownLoadNo);

		htHead2.put(Key.KEY_RECUSERID, recUserID);
		htHead2.put(Key.KEY_RECSESSIONID, recSessionID);
		htHead2.put(Key.KEY_RECPLAYTYPE, recPlaytype);
		htHead2.put(Key.KEY_RECMFUNCNO, recMFuncNo);
		htHead2.put(Key.KEY_RECSTATUS, recStatus);
		htHead2.put(Key.KEY_RECDOWNLOADNO, recDownLoadNo);
	}
	
	
	/**
	 * <p>获得解析后body部分信息</p>
	 * 
	 * @return allTmp 是未解析完全的结果，该结果需要通过具体业务来具体解析
	 * 
	 * @author Miko
	 */
	private String[] getBodyResult(){
		
		byte[] bytBody = new byte[rdyData.length - DataShare.FIRSTHEADLENGTH - DataShare.SECONDHEADLENGTH];
		
		
		// 提取返回数据的body部分
		System.arraycopy(rdyData, DataShare.FIRSTHEADLENGTH + DataShare.SECONDHEADLENGTH, bytBody, 0, bytBody.length);
		
		EDebug.o(this,"Getting Body From Server");
		
		String strBody = ByteUtils.bytesToString(bytBody);
		
		EDebug.o(this,"The body is : [" + strBody + "]");
		
		// 此处过滤掉了 服务器端用" "隔开字符串的方式，将其拆分正String[]
		byte[] bNullTmp = new byte[1];
		String strNullTmp = ByteUtils.bytesToString(bNullTmp, 0, 1);
		String[] allTmp = strBody.split(strNullTmp);
		for(int i=0;i<strNullTmp.length();i++){
			EDebug.o(this,strNullTmp);			
		}
		
		return allTmp;
	}

	// 为外部访问获得解析结果提供的 方法
	public String[] getResult(){
		return getBodyResult();
	}
	
	// 为外部访问解析head1部分结果提供的 方法
	public Hashtable<Object, Object> getHtHead1() {
		return htHead1;
	}


	// 为外部访问解析head2部分结果提供的 方法
	public Hashtable<Object, Object> getHtHead2() {
		return htHead2;
	}

	/** 
	* @Title: getReturnMsg 
	* @Description: TODO(得到返回信息) 
	* @return String    返回信息
	* @throws 
	*/
	public String getReturnMsg() {

        StringBuilder sbPrint = new StringBuilder();

        sbPrint.append(DataShare.LEFT_BR);

        for (int i = 0; i < getBodyResult().length; i++) {
            sbPrint.append(getBodyResult()[i]).append("|");
        }
        sbPrint.setLength(sbPrint.length() - 1);
        sbPrint.append(DataShare.RIGHT_BR);

        return sbPrint.toString();
    }
}
